<template>
  <van-tabbar route>
    <van-tabbar-item replace to="/home" icon="wap-home-o">首页</van-tabbar-item>
    <van-tabbar-item replace to="/list" icon="orders-o">列表</van-tabbar-item>
    <van-tabbar-item replace to="/shopCar" icon="shopping-cart-o" :badge="badge">购物车</van-tabbar-item>
    <van-tabbar-item replace to="/user" icon="user-o">会员中心</van-tabbar-item>
  </van-tabbar>
</template>

<script>
export default {
  data() {
    return {
      badge:0,
      goodsList:JSON.parse(localStorage.getItem("goodsList")) || []
      // JSON.parse(localStorage.getItem("goodsList")) || 
    };
  },
  methods: {
   
    getAllCount(list){
      let count = 0
      list.forEach((item)=>{
        count += item.num
      })
      return count
    }
  },
  watch:{
    goodsList:{
      handler(newVal){
        this.badge = this.getAllCount(newVal)
      },
      immediate:true
    }
  },
  created(){
    //this.getShopCarList()
    this.$eventBus.$on("getgoodsList",(res)=>{
      this.badge = this.getAllCount(res)
    })
  }
};
</script>

<style>
</style>